package com.erp.user.service.impl;

import com.erp.common.enums.DeleteEnum;
import com.erp.user.entity.Roles;
import com.erp.user.mapper.RolesMapper;
import com.erp.user.service.RolesService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.compress.utils.Lists;
import com.erp.common.response.ListVo;
import com.erp.common.utils.BeanUtils;
import com.erp.user.request.*;
import com.erp.user.response.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;


/**
 * @author shensong
 * @program machining_system.v1.0.0
 * @description  服务实现类
 * @date 2022/12/21 10:51:48
 */
@Service("rolesService")
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements RolesService {

    @Override
    public ListVo<List<RolesInfoResp>> index(RolesPageReq request) {
        LambdaQueryWrapper<Roles> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
            queryWrapper.eq(Objects.nonNull(request.getRoleId()),Roles::getRoleId, request.getRoleId());
            queryWrapper.eq(StringUtils.isNotBlank(request.getRoleName()),Roles::getRoleName, request.getRoleName());
            queryWrapper.eq(StringUtils.isNotBlank(request.getRoleCode()),Roles::getRoleCode, request.getRoleCode());
            queryWrapper.eq(Objects.nonNull(request.getType()),Roles::getType, request.getType());
            queryWrapper.eq(Objects.nonNull(request.getStatus()),Roles::getStatus, request.getStatus());
            queryWrapper.eq(StringUtils.isNotBlank(request.getRemark()),Roles::getRemark, request.getRemark());
            queryWrapper.eq(Objects.nonNull(request.getRevision()),Roles::getRevision, request.getRevision());
            queryWrapper.eq(Objects.nonNull(request.getGmtCreate()),Roles::getGmtCreate, request.getGmtCreate());
            queryWrapper.eq(Objects.nonNull(request.getGmtModified()),Roles::getGmtModified, request.getGmtModified());
        }
        queryWrapper.eq(Roles::getIsDeleted, DeleteEnum.NO.getCode());
        queryWrapper.orderByDesc(Roles::getRoleId);
        IPage<Roles> data = this.queryPage(request.getPageNum(), request.getPageSize(), queryWrapper);
        if (Objects.nonNull(data) && CollectionUtils.isNotEmpty(data.getRecords())) {
            List<RolesInfoResp> rolesListResponse = BeanUtils.copyObjects(data.getRecords(), RolesInfoResp.class);
            return new ListVo<>(rolesListResponse, data.getTotal(), data.getSize(), data.getCurrent());
        }

        return new ListVo<>();
    }

    @Override
    public Boolean store(RolesAddReq request) {
        Roles entity = BeanUtils.copyProperties(request, Roles.class);
        return this.save(entity);
    }

    @Override
    public Boolean update(RolesUpdateReq request) {
        Roles entity = BeanUtils.copyProperties(request, Roles.class);
        return this.updateById(entity);
    }

    @Override
    public Boolean destroy(RolesDeleteReq request) {
        LambdaUpdateWrapper<Roles> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(Roles::getIsDeleted, DeleteEnum.YES.getCode()).eq(Roles::getRoleId, request.getRoleId());
        return this.update(updateWrapper);
    }

    @Override
    public RolesInfoResp show(RolesInfoReq request) {
        LambdaQueryWrapper<Roles> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Roles::getRoleId, request.getRoleId());
        queryWrapper.eq(Roles::getIsDeleted, DeleteEnum.NO.getCode());
        Roles entity = this.getOne(queryWrapper);
        return Objects.nonNull(entity) ? BeanUtils.copyProperties(entity, RolesInfoResp.class) : null;
    }

    @Override
    public Boolean approve(RolesApproveReq request) {
        Roles entity = BeanUtils.copyProperties(request, Roles.class);
        return this.updateById(entity);
    }

    @Override
    public Boolean enabled(RolesStatusReq request) {
        Roles entity = BeanUtils.copyProperties(request, Roles.class);
        return this.updateById(entity);
    }

    @Override
    public List<RolesInfoResp> option(RolesListReq request) {
        LambdaQueryWrapper<Roles> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
            queryWrapper.eq(Objects.nonNull(request.getRoleId()), Roles::getRoleId, request.getRoleId());
            queryWrapper.eq(StringUtils.isNotBlank(request.getRoleName()), Roles::getRoleName, request.getRoleName());
            queryWrapper.eq(StringUtils.isNotBlank(request.getRoleCode()), Roles::getRoleCode, request.getRoleCode());
            queryWrapper.eq(Objects.nonNull(request.getType()), Roles::getType, request.getType());
            queryWrapper.eq(Objects.nonNull(request.getStatus()), Roles::getStatus, request.getStatus());
            queryWrapper.eq(Objects.nonNull(request.getIsDeleted()), Roles::getIsDeleted, request.getIsDeleted());
            queryWrapper.eq(StringUtils.isNotBlank(request.getRemark()), Roles::getRemark, request.getRemark());
            queryWrapper.eq(Objects.nonNull(request.getRevision()), Roles::getRevision, request.getRevision());
            queryWrapper.eq(Objects.nonNull(request.getGmtCreate()), Roles::getGmtCreate, request.getGmtCreate());
            queryWrapper.eq(Objects.nonNull(request.getGmtModified()), Roles::getGmtModified, request.getGmtModified());
        }

        queryWrapper.orderByDesc(Roles::getRoleId);
        List<Roles> data = this.list(queryWrapper);
        return CollectionUtils.isNotEmpty(data) ? BeanUtils.copyObjects(data, RolesInfoResp.class) : Lists.newArrayList();
    }

    @Override
    public List<RolesSearchInfoResp> search(RolesSearchReq request) {
        LambdaQueryWrapper<Roles> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
                queryWrapper.eq(Objects.nonNull(request.getRoleId()), Roles::getRoleId, request.getRoleId());
                queryWrapper.eq(StringUtils.isNotBlank(request.getRoleName()), Roles::getRoleName, request.getRoleName());
                queryWrapper.eq(StringUtils.isNotBlank(request.getRoleCode()), Roles::getRoleCode, request.getRoleCode());
                queryWrapper.eq(Objects.nonNull(request.getType()), Roles::getType, request.getType());
                queryWrapper.eq(Objects.nonNull(request.getStatus()), Roles::getStatus, request.getStatus());
                queryWrapper.eq(StringUtils.isNotBlank(request.getRemark()), Roles::getRemark, request.getRemark());
                queryWrapper.eq(Objects.nonNull(request.getRevision()), Roles::getRevision, request.getRevision());
                queryWrapper.eq(Objects.nonNull(request.getGmtCreate()), Roles::getGmtCreate, request.getGmtCreate());
                queryWrapper.eq(Objects.nonNull(request.getGmtModified()), Roles::getGmtModified, request.getGmtModified());
        }
        queryWrapper.eq(Roles::getIsDeleted, request.getIsDeleted());
        queryWrapper.orderByDesc(Roles::getRoleId);
        List<Roles> data = this.list(queryWrapper);
        return CollectionUtils.isNotEmpty(data) ? BeanUtils.copyObjects(data, RolesSearchInfoResp.class) : Lists.newArrayList();
    }

    @Override
    public RolesDetailInfoResp detail(RolesInfoReq request) {
        return BeanUtils.copyProperties(this.show(request), RolesDetailInfoResp.class);
    }

    private IPage<Roles> queryPage(Long pageNum, Long pageSize, Wrapper<Roles> queryWrapper) {
        IPage<Roles> selectPage = new Page<>();
        selectPage.setCurrent(pageNum);
        selectPage.setSize(pageSize);

        IPage<Roles> page = this.baseMapper.selectPage(selectPage, queryWrapper);
        return page;
    }
}
